package com.zxy.allSearcher.manager;

import com.zhipu.oapi.ClientV4;
import com.zhipu.oapi.Constants;
import com.zhipu.oapi.service.v4.model.*;
import io.reactivex.Flowable;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Ai请求生成器
 *
 * @author zxy
 * @since 2024/6/20
 **/
@Component
public class AiManager {
    // 专业型
    public final Float goodTem = 0.9f;
    // 发散型
    public final Float badTem = 0.2f;
    @Resource
    private ClientV4 clientV4;

    /**
     * 通用流式请求(简化)
     */
    public Flowable<ModelData> doStreamReq(String sysMsg,String userMsg, Float temperature){
        ArrayList<ChatMessage> chatMsg = new ArrayList<>(3);
        chatMsg.add(new ChatMessage(ChatMessageRole.SYSTEM.value(), sysMsg));
        chatMsg.add(new ChatMessage(ChatMessageRole.USER.value(), userMsg));
        return doStreamReq(chatMsg,temperature);
    }
    /**
     * 通用流式请求
     */
    public Flowable<ModelData> doStreamReq(List<ChatMessage> msg, Float temperature){
        ChatCompletionRequest chatReq = ChatCompletionRequest.builder()
                .model(Constants.ModelChatGLM4)
                .stream(Boolean.TRUE)
                .temperature(temperature)
                .invokeMethod(Constants.invokeMethod)
                .messages(msg)
                .build();
        ModelApiResponse invoke = clientV4.invokeModelApi(chatReq);
        return invoke.getFlowable();
    }
}
